package com.zjapl.web.dao.impl;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.zjapl.web.action.vo.Employee;
import com.zjapl.web.dao.EmployeeDao;


/**
 * 
* @author linmeng E-mail:kevinlinm1989@gmail.com
* @version 创建时间：2013-1-10 下午4:13:55
* 类说明
 */
public class EmployeeDaoImpl implements EmployeeDao {
	private static SqlMapClient sqlMapClient = null;

	// 读取配置文件
	static {
		try {
			Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
			sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	

	  /**
	   * 该方法实现用户的登录
	   * @param user
	   * @return
	 * @throws SQLException 
	   */
	  @Override
    public Employee Login(Employee emp) throws SQLException {
		  // TODO Auto-generated method stub
		  return   (Employee) sqlMapClient.queryForObject("Login", emp);
		
	  }
	  
	// 检测用户名是否存在
	  
		public boolean loginCheckEmail(String email) {
			try {
				if (sqlMapClient.queryForObject("LoginCheckEmail", email) != null) {
					return true;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return false;
		}
		
		public boolean resetPassWord(Employee empSet){
			try{
				sqlMapClient.update("ResetPasswod", empSet);
				
			}catch(Exception e){
				e.printStackTrace();
			}
			return true;
		}
		
		public boolean addEmployee(Employee emp){
			try{
				if(sqlMapClient.insert("AddEmployee", emp)!=null){  //判断得到的对象是否为空，不为空返回true
					
					 return true;
				 }			 
			  }catch(SQLException e){
				  e.printStackTrace();
			  }
			   return false;
		}
		
		public boolean deleteEmployee(int emp_id){
			try{
				sqlMapClient.delete("DeleteEmployee", emp_id);
					
					 return true;
				 		 
			  }catch(Exception e){
					e.printStackTrace();
				}
			   return false;
		}

		@Override
		public boolean uodateEmployee(Employee emp) {
			// TODO Auto-generated method stub
			try{
				sqlMapClient.update("UpdateEmployee", emp);	
				
			  }catch(SQLException e){
				  e.printStackTrace();
			  }
			return true;
		}
}
